<?php
namespace app\index\controller;

use think\Controller;
use think\Request;

class Lists extends Base
{
    public function index(Request $request)
    {
        $firstCatIds = [];
        $categorys = model('Category')->getNormalCategorysByParentId();
        foreach ($categorys as $category) {
            $firstCatIds[] = $category['id'];
        }
        $id = $request->param('id', 0, 'intval');
        $data = [];
        if (in_array($id, $firstCatIds))
        {
            // 一级分类
            $categoryParentId = $id;
            $data['category_id'] = $id;
        }
        else if ($id)
        {
            // 获取二级分类数据
            $category = model('Category')->get($id);
            if (!$category || $category['status'] != 1)
            {
                $this->error('数据不合法');
            }
            $categoryParentId = $category['parent_id'];
            $data['se_category_id'] = $id;
        }
        else
        {
            // 0
            $categoryParentId = 0;
        }
        $sedcategorys = [];
        // 获取父类下的所有子分类
        if ($categoryParentId)
        {
            $sedcategorys = model('Category')->getNormalCategorysByParentId($categoryParentId);
        }
        $orders = [];
        // 排序
        $order_sales = $request->param('order_sales');
        $order_price = $request->param('order_price');
        $order_time = $request->param('order_time');
        if (!empty($order_sales))
        {
            $orderflag = 'order_sales';
            $orders['order_sales'] = $order_sales;
        }
        elseif (!empty($order_price))
        {
            $orderflag = 'order_price';
            $orders['order_price'] = $order_price;
        }
        elseif (!empty($order_time))
        {
            $orderflag = 'order_time';
            $orders['order_time'] = $order_time;
        }
        else
        {
            $orderflag = '';
        }
        $data['city_id'] = $this->city['id'];
        // 根据order条件来查询商品列表数据
        $deals = model('Deal')->getDealByConditions($data, $orders);

        $this->assign([
            'categorys' => $categorys,
            'sedcategorys' => $sedcategorys,
            'id' => $id,
            'categoryParentId' => $categoryParentId,
            'orderflag' => $orderflag,
            'deals' => $deals,
        ]);
        return $this->fetch();
    }
}